﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FAS.App_Code;
using FAS.Objetos;
using FAS;
using System.Threading;

namespace FasGUI
{
    public partial class FormMaterias : Form
    {
        private Form inicio;
        private List<int> IdViejos = new List<int>();
        public FormMaterias(Form inicio)
        {
            this.inicio = inicio;
            InitializeComponent();
            this.Show();
            fillAll();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            fillAll();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            new NuevaMateria(this).Show();
            fillAll();
        }

        private DataGridViewTextBoxCell getCell(String value)
        {
            DataGridViewTextBoxCell celda = new DataGridViewTextBoxCell();
            celda.Value = value;
            return celda;
        }

        public static byte[] ToByteArray(string cadena)
        {

            System.Text.ASCIIEncoding codificador = new System.Text.ASCIIEncoding();
            return codificador.GetBytes(cadena);

        }

        public void fillAll()
        {
            this.IdViejos.Clear();
            ConexionMySQL conexion = new ConexionMySQL();
            DataSet data = conexion.GetQuery("SELECT * FROM materia");
            this.dataGridView1.Rows.Clear();
            int x = 0;
            foreach(DataRow rowD in data.Tables[0].Rows)
            {
                string IdMateriaStr = rowD["IdMateria"].ToString();
                string ClaveMateriaStr = rowD["ClaveMateria"].ToString();
                string NombreStr = rowD["Nombre"].ToString();
                
                this.IdViejos.Add(int.Parse(IdMateriaStr));
                DataGridViewRow row = new DataGridViewRow();
                row.Cells.Add(getCell(IdMateriaStr));
                row.Cells.Add(getCell(ClaveMateriaStr));
                row.Cells.Add(getCell(NombreStr));
                DataGridViewLinkCell l = new DataGridViewLinkCell();
                l.Value = "Ver";
                row.Cells.Add(l);
                DataGridButtonCellPayo button = new DataGridButtonCellPayo(int.Parse(IdMateriaStr));
                button.Value = "Eliminar";
                row.Cells.Add(button);
                this.dataGridView1.Rows.Add(row);
                x++;
            }
            this.dataGridView1.Columns[1].AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.ColumnHeader;
            this.dataGridView1.Columns[1].AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
            this.dataGridView1.Columns[2].AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.ColumnHeader;
            this.dataGridView1.Columns[2].AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if(this.Validate()){
                for(int r=0;r<this.dataGridView1.Rows.Count;r++)
                {
                    int id = int.Parse(this.dataGridView1.Rows[r].Cells[0].Value.ToString());
                    DataSet data = new ConexionMySQL().GetQuery("SELECT * FROM `materia` where IdMateria = '" + id + "'");
                    Materia materia = new Materia();
                    materia.IdMateria = id;
                    materia.Nombre = data.Tables[0].Rows[0]["Nombre"].ToString();
                    materia.ClaveMateria = (data.Tables[0].Rows[0]["ClaveMateria"].ToString());
                    materia.Nombre = Uri.UnescapeDataString(materia.Nombre);
                    materia.ClaveMateria = (this.dataGridView1.Rows[r].Cells[1].Value.ToString());
                    materia.Nombre = (this.dataGridView1.Rows[r].Cells[2].Value.ToString());
                    materia.update();
                }
                fillAll();
                MessageBox.Show("Se Han Actualizado los Datos");
            }
            else MessageBox.Show("Error: Hay Objetivos Vacios");
            
        }

        private new bool Validate()
        {
            for (int r = 0; r < IdViejos.Count; r++)
            {
                object s = (this.dataGridView1.Rows[r].Cells[1].Value);
                if (s == null)
                    return false;
            }
            return true;
        }

        private void clickdgv(object sender, DataGridViewCellEventArgs e)
        {
            int row = dataGridView1.SelectedCells[0].RowIndex;    
            if(e.ColumnIndex == 3){
                this.Hide();
                new FormFiltroCatalogo(this,int.Parse(this.dataGridView1.Rows[row].Cells[0].Value.ToString())).Show();
            }
            else if(e.ColumnIndex == 4)
            {
                Confirm c = new Confirm(this, row);
                c.Show();
            }
        }

        private void FormFiltroCatalogo_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Dispose();
            inicio.Show();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Dispose();
            inicio.Show();
        }

        internal void validaPass(string p,int row)
        {
            if (p.Equals(Auxiliar.getPass()))
            {
                int id = int.Parse(this.dataGridView1.Rows[row].Cells[0].Value.ToString());
                String clave = this.dataGridView1.Rows[row].Cells[1].Value.ToString();
                String nombre = this.dataGridView1.Rows[row].Cells[2].Value.ToString();
                Materia m = new Materia();
                m.ClaveMateria = clave;
                m.IdMateria = id;
                m.Nombre = nombre;
                m.delete();
                fillAll();
                MessageBox.Show("Se Ha Borrado la Materia");
            }
            else MessageBox.Show("Password Incorrecta");
        }
    }
}